package com.server.commons.file.factory;

import com.server.model.all.enums.BucketNameEnum;
import com.server.common.code.error.BusinessException;
import com.server.common.code.utils.OptionalUtils;
import com.server.commons.file.interfaces.FileFactoryInterface;
import com.server.commons.file.interfaces.FileInterface;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件服务器工厂
 * @author LXC
 * @date 2022/6/4 2:33
 */
@Component
public class FileFactory implements FileFactoryInterface {

    private static final Map<String, FileInterface> fileInterfaceFactory = new HashMap<>();

    @Override
    public FileInterface getFileInterface(BucketNameEnum bucketNameEnum){
        FileInterface fileInterface = fileInterfaceFactory.get(bucketNameEnum.getName());
        OptionalUtils.ofEmpty(fileInterface).orElseThrow(()->new BusinessException("暂无不支持此文件系统"));
        return fileInterface;
    }

    /**
     * 注册
     */
    public void register(BucketNameEnum bucketNameEnum, FileInterface fileInterface) {
        if (!ObjectUtils.isEmpty(bucketNameEnum)) {
            fileInterfaceFactory.put(bucketNameEnum.getName(), fileInterface);
        }
    }
}
